//
//  QJNearVCHeaderJumpVC.m
//  QuanJieShopProject
//
//  Created by 刘中义 on 2018/2/12.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJNearVCHeaderJumpVC.h"
//数据为空
#import "UIScrollView+EmptyDataSet.h"

#import "QJNearVCTCell.h"
#import "QJNearVCShopDetailVC.h"
#import "YFSearchBar.h"
@interface QJNearVCHeaderJumpVC () <UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource,UISearchBarDelegate>
@property (nonatomic, strong) UITableView *jumpTableView;
@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) NSString *selectPaiXu;
@property (nonatomic, strong) NSString *searchStr;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, strong)UISearchBar *searchBar;

@end

@implementation QJNearVCHeaderJumpVC

- (void)viewDidLoad {
    [super viewDidLoad];


    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH - 100, 30)];//allocate titleView
    [titleView setBackgroundColor:[UIColor clearColor]];
    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.frame = CGRectMake(0, 0, UISCREEN_WIDTH - 100, 30);
    searchBar.backgroundColor = [UIColor clearColor];
    searchBar.delegate = self;
    searchBar.placeholder = @"请输入搜索内容";
    searchBar.barStyle = UISearchBarStyleDefault;
    [titleView addSubview:searchBar];
    self.searchBar = searchBar;
    self.navigationItem.titleView = titleView;
    
    //设置背景图是为了去掉上下黑线
    searchBar.backgroundImage = [[UIImage alloc] init];
    // 设置SearchBar的颜色主题为白色
    searchBar.barTintColor = [UIColor whiteColor];
    searchBar.backgroundColor = [UIColor whiteColor];
    [self addRightBtn];
    [self defaultUIPrepareAction];
    [self setupClassUIAction];
    
}

- (void)addRightBtn {
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    [rightBarItem setImage:[UIImage imageNamed:@"HSearch"]];
    [rightBarItem setTintColor:[UIColor whiteColor]];
    //    [rightBarItem setTintColor:[UIColor lightGrayColor]];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)onClickedOKbtn {
    [self.searchBar resignFirstResponder];
    self.pageNumber = 1;
    [self requestParameterPrepareActionWithBtn:self.selectBtn];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //数据请求
    [self nearHeaderJumpVCDataRequestAction];
    if (IOS_VERSION < 11.0) {
        self.navTopHeight.constant = 20;
    }else{
        
        self.navTopHeight.constant = 0;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavAlpha:NO];
    [self.searchBar resignFirstResponder];
}

#pragma mark -- 按钮点击方法
- (void)searchClick {
    self.searchStr = self.searchBar.text;
    self.pageNumber = 1;
    [self nearHeaderJumpVCDataRequestAction];
}
#pragma mark -- 默认配置
- (void)defaultUIPrepareAction{
    self.searchStr = @"";
    self.selectPaiXu = @"0";
    self.pageNumber = 1;
    self.zhinengBtn.selected = YES;
    self.zhinengBtn.tag = 302;
    self.selectBtn = self.zhinengBtn;
}
#pragma mark -- 界面布局
- (void)setupClassUIAction{
    //修改placeholder颜色
//    self.searchTF = [UITextField changeTextField:self.searchTF PlaceholderColorWith:HexRGB(0x666666) WithFont:15];
    
    self.jumpTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - 100) style:UITableViewStylePlain];
    [self.jumpView addSubview:self.jumpTableView];
    self.jumpTableView.delegate = self;
    self.jumpTableView.dataSource = self;
    self.jumpTableView.emptyDataSetDelegate = self;
    self.jumpTableView.emptyDataSetSource = self;
    self.jumpTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.jumpTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(HRefresh)];
    self.jumpTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(FRefresh)];
}
#pragma mark -- 顶部按钮点击方法
- (IBAction)topBtnClickAction:(UIButton *)sender {
    NSInteger tag = sender.tag;
    switch (tag) {
        case 300:
        {
            KPopViewControllerAction;
        }
            break;
        case 301:
        {
            if (self.searchTF.text.length < 1) {
                self.searchStr = @"";
            }else{
                self.searchStr = self.searchTF.text;
            }
            [self.searchTF resignFirstResponder];
            self.pageNumber = 1;
            [self nearHeaderJumpVCDataRequestAction];
        }
            break;
            
        default:
            [self requestParameterPrepareActionWithBtn:sender];
            break;
    }
}
#pragma mark -- 搜索数据参数配置
- (void)requestParameterPrepareActionWithBtn:(UIButton *)sender{
    NSInteger selectTag = self.selectBtn.tag;
    NSInteger senderTag = sender.tag;
    if (selectTag == senderTag) {
        KMyLog(@"点击的是同一个按钮");
    }else{
        self.selectBtn.selected = NO;
        sender.selected = YES;
        self.selectBtn = sender;
        switch (senderTag) {
            case 202:
            {
                self.selectPaiXu = @"0";
            }
                break;
            case 203:
            {
                self.selectPaiXu = @"1";
            }
                break;
            default:
            {
                self.selectPaiXu = @"2";
            }
                break;
        }
        self.pageNumber = 1;
        [self nearHeaderJumpVCDataRequestAction];
    }
}
#pragma mark -- 数据请求方法
- (void)nearHeaderJumpVCDataRequestAction{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.classID forKey:@"categoryNearId"];
    [dict setObject:self.searchStr forKey:@"keywords"];
    [dict setObject:self.selectPaiXu forKey:@"order"];
    [dict setObject:[NSString stringWithFormat:@"%ld",(long)self.pageNumber] forKey:@"page"];
    [dict setObject:self.longitudeStr forKey:@"longitude"];
    [dict setObject:self.latitudeStr forKey:@"latitude"];
    
    NSLog(@"商铺列表 dict = %@", dict);
    
    [WWNetRequest fetchShopListWithParameters:dict success:^(id response) {
        NSLog(@"商铺列表 response = %@", response);
        if (self.pageNumber == 1) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *data = response[@"data"][@"shop"][@"data"];
        [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NearShopModel *model = [[NearShopModel alloc]initWithDictionary:obj error:nil];
            [self.dataSourceArray addObject:model];
        }];
        [self.jumpTableView reloadData];
    } failure:^(NSDictionary *error) {
        NSLog(@"商铺列表 error = %@", error);

    }];

    [self.jumpTableView.mj_header endRefreshing];
    [self.jumpTableView.mj_footer endRefreshing];
}
#pragma mark -- 上拉刷新方法
- (void)HRefresh{
    self.pageNumber = 1;
    [self nearHeaderJumpVCDataRequestAction];
}
#pragma mark -- 下拉加载方法
- (void)FRefresh{
    self.pageNumber++;
    [self nearHeaderJumpVCDataRequestAction];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"Cell";
    QJNearVCTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"QJNearVCTCell" owner:self options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NearShopModel *model = self.dataSourceArray[indexPath.row];
    cell.cellDistanceLabel.hidden = YES;
    [cell cellUpdateWithModel:model];
    return cell;
}
#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    QJNearVCShopDetailVC *VC = [[QJNearVCShopDetailVC alloc]init];
    NearShopModel *model = self.dataSourceArray[indexPath.row];
    VC.shopID = model.id;
    KPushViewControllerAction(VC);
}
#pragma mark -- DZNEmptyDataSetSource
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:@"noDataImage"];
}
#pragma mark -- DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
#pragma mark -- 懒加载
- (NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.searchBar resignFirstResponder];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    //已经开始编辑
}// called when text starts editing

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"收键盘监听");
    
}// called when text ends editing


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    //searchText是searchBar上的文字 每次输入或删除都都会打印全部
    self.pageNumber = 1;
    _searchStr = searchText;
}

//点击键盘上搜索时的相应事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
    self.pageNumber = 1;
    [self nearHeaderJumpVCDataRequestAction];
}// called when keyboard search button pressed



@end
